﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Soundelle;
using Soundelle.Oscillator;

namespace Soundelle.SignalGenerator
{
    public partial class Form1 : Form
    {
        Oscillator.BaseDevice Device = new Oscillator.BaseDevice();
        private int BlockCount = 10;

        private SoundPlayer.WavFilePlayer WavPlay = new SoundPlayer.WavFilePlayer();
        private string WavFileFilename = @"c:\temp\temp.wav";

        
        public Form1()
        {
            InitializeComponent();
            this.Text = VersionInfo.GetVersionString(VersionInfo.VersionStringStyle.VersionShortStyle);

            StatusLabelDevice.Text = "";

            panelMenu.Dock = DockStyle.Bottom;
            splitMain.Dock = DockStyle.Fill;
            chartSignal.Dock = DockStyle.Fill;
            DeviceTree.Dock = DockStyle.Fill;

            DeviceTree.AttachDevice(ref Device);
        }

        private void DeviceTree_DeviceChanged(object sender, EventArgs e)
        {
            StatusLabelDevice.Text = DeviceTree.SelectedDevice.GetType().ToString() + ": " + DeviceTree.SelectedDevice.GUID.ToString();
        }

        private void ComboSamplingRate_TextChanged(object sender, EventArgs e)
        {
            double value = 0.0;

            if (double.TryParse(ComboSamplingRate.Text, out value))
            {
                ComboSamplingRate.BackColor = Color.White;
                Device.SamplingRate = value;
            }
            else
            {
                ComboSamplingRate.BackColor = Color.LightPink;
            }
        }

        private void ComboBlocks_TextChanged(object sender, EventArgs e)
        {
            int value = 0;

            if (int.TryParse(ComboBlocks.Text, out value))
            {
                ComboBlocks.BackColor = Color.White;
                BlockCount = value;
            }
            else
            {
                ComboBlocks.BackColor = Color.LightPink;
            }
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            btnPlay.Enabled = false;
            btnStop.Enabled = false;

            System.Windows.Forms.Application.DoEvents();

            Device.Reset();
            long sample = (long)Device.SamplingRate;
            double[] Buffer = new double[BlockCount * sample];

            Oscillator.BaseDevice dev = DeviceTree.SelectedDevice;

            for (int i = 0; i < BlockCount; ++i)
            {
                for (int j = 0; j < Device.SamplingRate; ++j)
                {
                    Buffer[i * sample + j] = dev.SignalOut();
                }
            }

            SoundPlayer.WavFileCreator.WriteWav16(ref Buffer, WavFileFilename, sample, 5000.0);

            btnPlay.Enabled = true;
            System.Windows.Forms.Application.DoEvents();
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            btnPlay.Enabled = false;
            btnStop.Enabled = true;
            WavPlay.PlaySound(WavFileFilename, true);
            System.Windows.Forms.Application.DoEvents();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            btnPlay.Enabled = true;
            btnStop.Enabled = false;
            WavPlay.StopSound();
            System.Windows.Forms.Application.DoEvents();
        }

        private void menuHelp_Click(object sender, EventArgs e)
        {
            string message = VersionInfo.GetVersionString(VersionInfo.VersionStringStyle.VersionLongStyle);
            message += "\n\n";
            message += global::Soundelle.SignalGenerator.Properties.Resources.EULA;
            MessageBox.Show(message,"About this application...");
        }

    }
}
